/** 
 * 文 件 名:WebServiceRequest.java
 * Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
 * 创 建 人:Administrator
 * 日    期:2012-3-13 上午10:14:28
 * 修 改 人: 
 * 日   期:
 * 修改描述:
 * 描   述:TODO
 */
package net.seehealth.healthpad.application.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;


import net.seehealth.healthpad.application.util.DataServiceHelper;

/**
 * @className WebServiceRequest
 * @author 林洪波
 * @description TODO
 * @date 2012-3-13 上午10:14:28
 */
public class WebServiceManager {

	// 请求地址
	public static final String URL = "http://192.168.2.100:9094/TPCWebService.asmx";

	// 服务器响应成功
	private static final int OK = 200;

	// 请求超时时间设置
	private static final int REQUEST_TIME_OUT = 10 * 1000;

	//等待数据时间设置
	private static final int WAIT_DATA_TIME_OUT = 10 * 1000;
	
	private WebServiceManager() {

	}

	/**
	 * @description:TODO
	 * @param
	 * @return 返回数据
	 * @throws Exception
	 * @author TODO
	 * @date 2012-3-13 下午12:54:40
	 */
	public static String getResponseSoap(String request,String url) throws Exception {
		URL Url = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
		connection.setRequestMethod("POST");
		connection.setConnectTimeout(REQUEST_TIME_OUT);
		connection.setDoOutput(true);
		connection.setRequestProperty("Content-Type",
				"application/soap+xml; charset=utf-8");
		byte[] data = request.getBytes();

		connection.setRequestProperty("Content-Length",
				String.valueOf(data.length));
		connection.setReadTimeout(WAIT_DATA_TIME_OUT);
		
		OutputStream outStream = connection.getOutputStream();
		outStream.write(data);
		outStream.flush();
		outStream.close();

		if (connection.getResponseCode() == OK) {
			InputStream is = connection.getInputStream();
			byte[] result = DataServiceHelper.readInputStream(is);
			String responseSoap = new String(result);
			return responseSoap;
		}

		return null;
	}

	/**
	 * @description:TODO
	 * @param
	 * @return 返回数据
	 * @throws Exception
	 * @author TODO
	 * @date 2012-3-13 下午12:54:40
	 */
	public static String getResponseSoapBodyString(final String requestSoap,String url) throws Exception {
		InputStream is=WebServiceManager.getResponseSoapBodyInput(requestSoap,url);
		if(is!=null)
		{
			byte[] result=DataServiceHelper.readInputStream(is);
			String bodyContent=new String(result,"utf-8");
			return bodyContent;
		}
		return null;
		
	}
	
	/**
	 * @description:TODO
	 * @param
	 * @return 返回数据
	 * @throws Exception
	 * @author TODO
	 * @date 2012-3-13 下午12:54:40
	 */
	public static InputStream getResponseSoapBodyInput(final String requestSoap,String url) throws Exception {
		BasicHttpParams httpParams = new BasicHttpParams();  

		HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIME_OUT);  

		HttpConnectionParams.setSoTimeout(httpParams,WAIT_DATA_TIME_OUT);  

		HttpClient client = new DefaultHttpClient(httpParams);
		
		HttpPost post = new HttpPost(url);
		
		ContentProducer cp = new ContentProducer() {
			public void writeTo(OutputStream outstream) throws IOException{
				Writer writer = new OutputStreamWriter(outstream, "UTF-8");
				writer.write(requestSoap);
				writer.flush();
			}
		};
		Header soapHead=new Header() {
			
			public String getValue() {
				// TODO Auto-generated method stub
				return "application/soap+xml; charset=utf-8";
			}
			
			public String getName() {
				// TODO Auto-generated method stub
				return "Content-Type";
			}
			
			public HeaderElement[] getElements() throws ParseException {
				// TODO Auto-generated method stub
				return null;
			}
		};
		post.setHeader(soapHead);
		post.setEntity(new EntityTemplate(cp));
		HttpResponse response = client.execute(post); 
		
		if(response!=null&&response.getStatusLine().getStatusCode()==OK)
		{
			InputStream is=response.getEntity().getContent();
			return is;
		}
		return null;
		
	}
	
	
}
